﻿using System;
using System.Runtime.Serialization;

namespace MyFramework.Core
{
    /// <summary>
    /// controller exception
    /// </summary>
    [Serializable]
    public class ControllerException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        public ControllerException()
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        /// <param name="message">The message that describes the error.</param>
        public ControllerException(string message)
            : base(message)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="controllerId">The controller unique identifier.</param>
        /// <param name="actionName">Name of the action.</param>
        public ControllerException(string message, Guid controllerId, string actionName)
            : base(message)
        {
            this.ControllerId = controllerId;
            this.ActionName = actionName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="inner">The inner.</param>
        public ControllerException(string message, Exception inner)
            : base(message, inner)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="controllerId">The controller unique identifier.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="inner">The inner.</param>
        public ControllerException(string message, Guid controllerId, string actionName, Exception inner)
            : base(message, inner)
        {
            this.ControllerId = controllerId;
            this.ActionName = actionName;
        }

        /// <summary>
        /// Gets the controller unique identifier.
        /// </summary>
        /// <value>
        /// The controller unique identifier.
        /// </value>
        public Guid ControllerId { get; private set; }

        /// <summary>
        /// Gets the name of the action.
        /// </summary>
        /// <value>
        /// The name of the action.
        /// </value>
        public string ActionName { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        protected ControllerException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        { }

        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter" />
        ///   </PermissionSet>
        /// <exception cref="System.ArgumentNullException">info</exception>
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            base.GetObjectData(info, context);
            info.AddValue("ControllerActionName", this.ActionName, typeof(string));
            info.AddValue("ControllerControllerId", this.ControllerId != null ? this.ControllerId.ToString() : "n.d.", typeof(string));
        }

    }
}
